/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ConfigDlg.java
 *
 * Created on Jul 19, 2011, 10:01:22 PM
 */
package com.viweb.storemanager.ui.config;

import com.viweb.storemanager.common.ButtonBarFactory;
import com.viweb.storemanager.core.CursorManager;
import com.viweb.storemanager.core.OptionPane;
import com.viweb.storemanager.entity.ConfigManager;
import com.viweb.storemanager.ui.MainFrm;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JOptionPane;
import javax.swing.JToggleButton;

/**
 *
 * @author qhvic
 */
public class ConfigDlg extends javax.swing.JDialog implements ActionListener {

    private ButtonBarFactory buttonBarFactory;
    private JToggleButton btnConfigDatabase, btnConfigInfo;
    private ConfigDatabase pnlConfigDatabase;
    private ConfigInfoPnl pnlConfigInfo;
    private Component currentComponent;

    /** Creates new form ConfigDlg */
    public ConfigDlg(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        this.pnlConfigDatabase = new ConfigDatabase();
        this.pnlConfigInfo = new ConfigInfoPnl();

        this.buttonBarFactory = new ButtonBarFactory("/com/viweb/storemanager/imgae/");

        this.btnConfigDatabase = this.buttonBarFactory.addButton("Thông tin", "printing.png");
        this.btnConfigDatabase.setSelected(true);
        this.btnConfigDatabase.addActionListener(this);

        this.btnConfigInfo = this.buttonBarFactory.addButton("Database", "printing.png");
        this.btnConfigInfo.setSelected(true);
        this.btnConfigInfo.addActionListener(this);

        initComponents();

        this.pnlBar.add(buttonBarFactory.getButtonBar());
        this.pnlConfigPane.add(this.pnlConfigDatabase);
        this.currentComponent = this.pnlConfigDatabase;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        java.awt.GridBagConstraints gridBagConstraints;

        pnlConfigPane = new javax.swing.JPanel();
        pnlBar = new javax.swing.JPanel();
        btnOK = new javax.swing.JButton();
        btnCancel = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Thiết lập thông tin");
        getContentPane().setLayout(new java.awt.GridBagLayout());

        pnlConfigPane.setLayout(new java.awt.BorderLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridwidth = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.weighty = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 0, 5);
        getContentPane().add(pnlConfigPane, gridBagConstraints);

        pnlBar.setLayout(new java.awt.BorderLayout());
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 0;
        gridBagConstraints.gridy = 0;
        gridBagConstraints.gridheight = 2;
        gridBagConstraints.fill = java.awt.GridBagConstraints.BOTH;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(pnlBar, gridBagConstraints);

        btnOK.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/viweb/storemanager/imgae/ok.png"))); // NOI18N
        btnOK.setText("Đồng ý");
        btnOK.setMaximumSize(new java.awt.Dimension(90, 25));
        btnOK.setMinimumSize(new java.awt.Dimension(90, 25));
        btnOK.setPreferredSize(new java.awt.Dimension(90, 25));
        btnOK.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnOKActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 1;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.anchor = java.awt.GridBagConstraints.EAST;
        gridBagConstraints.weightx = 1.0;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 0);
        getContentPane().add(btnOK, gridBagConstraints);

        btnCancel.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/viweb/storemanager/imgae/media_stop_red.png"))); // NOI18N
        btnCancel.setText("Hủy bỏ");
        btnCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelActionPerformed(evt);
            }
        });
        gridBagConstraints = new java.awt.GridBagConstraints();
        gridBagConstraints.gridx = 2;
        gridBagConstraints.gridy = 1;
        gridBagConstraints.insets = new java.awt.Insets(5, 5, 5, 5);
        getContentPane().add(btnCancel, gridBagConstraints);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnOKActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnOKActionPerformed
        // TODO add your handling code here:
        CursorManager.setWaitCursor(this);
        if (!pnlConfigDatabase.updateConfig()) {
            OptionPane.show(JOptionPane.getFrameForComponent(ConfigDlg.this),
                    "Lưu cấu hình không thành công! "
                    + pnlConfigDatabase.getValidDescription(), "Thông báo",
                    OptionPane.MsgTYPE_WARNING);
            return;
        }

        new ConfigManager().writeConfig(MainFrm.APP_CONFIG);
        this.dispose();

        CursorManager.setDefaultCursor(this);
    }//GEN-LAST:event_btnOKActionPerformed

    private void btnCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnCancelActionPerformed

    @Override
    public void setVisible(boolean isVisible) {
        this.pnlConfigDatabase.requestFocus();
        super.setVisible(isVisible);
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancel;
    private javax.swing.JButton btnOK;
    private javax.swing.JPanel pnlBar;
    private javax.swing.JPanel pnlConfigPane;
    // End of variables declaration//GEN-END:variables

    public void actionPerformed(ActionEvent e) {
        this.pnlConfigPane.remove(this.currentComponent);

        if (e.getSource() == this.btnConfigDatabase) {
            this.pnlConfigPane.add(this.pnlConfigDatabase, java.awt.BorderLayout.CENTER);
            this.currentComponent = this.pnlConfigDatabase;
        } else if (e.getSource() == this.btnConfigInfo) {
            this.pnlConfigPane.add(this.pnlConfigInfo, java.awt.BorderLayout.CENTER);
            this.currentComponent = this.pnlConfigInfo;

        }

        this.pnlConfigPane.revalidate();
        this.pnlConfigPane.repaint();
    }
}
